using System.Windows.Browser;

namespace HtmlUtilities
{
    public interface IHtmlElement : IHtmlObject
    {
        void AppendChild(IHtmlElement element);
        void AppendChild(IHtmlElement element, IHtmlElement referenceElement);
        IScriptObjectCollection Children { get; }
        string CssClass { get; set; }
        void Focus();
        string GetAttribute(string name);
        string GetStyleAttribute(string name);
        string Id { get; set; }

        IHtmlElement Parent { get; }
        void RemoveAttribute(string name);
        void RemoveChild(IHtmlElement child);
        void RemoveStyleAttribute(string name);
        void SetAttribute(string name, string value);
        void SetStyleAttribute(string name, string value);
        string TagName { get; }
    }

    internal class HtmlElementWrapper : HtmlObjectWrapper, IHtmlElement
    {
        private readonly HtmlElement _htmlElement;
        public HtmlElementWrapper(HtmlElement element)
            : base(element)
        {
            _htmlElement = element;
        }

        internal HtmlElement Element { get { return _htmlElement; } }

        public void AppendChild(IHtmlElement element)
        {
            _htmlElement.AppendChild((element as HtmlElementWrapper).Element);
        }

        public void AppendChild(IHtmlElement element, IHtmlElement referenceElement)
        {
            _htmlElement.AppendChild((element as HtmlElementWrapper).Element, (referenceElement as HtmlElementWrapper).Element);
        }

        public IScriptObjectCollection Children
        {
            get { return ScriptObjectFactory.GetScriptObject<IScriptObjectCollection>(_htmlElement.Children); }
        }

        public string CssClass
        {
            get { return _htmlElement.CssClass; }
            set { _htmlElement.CssClass = value; }
        }

        public void Focus()
        {
            _htmlElement.Focus();
        }

        public string GetAttribute(string name)
        {
            return _htmlElement.GetAttribute(name);
        }

        public string GetStyleAttribute(string name)
        {
            return _htmlElement.GetStyleAttribute(name);
        }

        public string Id
        {
            get { return _htmlElement.Id; }
            set { _htmlElement.Id = value; }
        }

        public IHtmlElement Parent
        {
            get { return ScriptObjectFactory.GetScriptObject<IHtmlElement>(_htmlElement.Parent); }
        }

        public void RemoveAttribute(string name)
        {
            _htmlElement.RemoveAttribute(name);
        }

        public void RemoveChild(IHtmlElement child)
        {
            _htmlElement.RemoveChild((child as HtmlElementWrapper).Element);
        }

        public void RemoveStyleAttribute(string name)
        {
            _htmlElement.RemoveStyleAttribute(name);
        }

        public void SetAttribute(string name, string value)
        {
            _htmlElement.SetAttribute(name, value);
        }

        public void SetStyleAttribute(string name, string value)
        {
            _htmlElement.SetStyleAttribute(name, value);
        }

        public string TagName
        {
            get { return _htmlElement.TagName; }
        }
    }
}